#import "FlutterBdfacePlugin.h"
#import "NSObject+BdTransFlutterStreamManager.h"
#import "IDLFaceSDK/IDLFaceSDK.h"
#import "BDFaceLivenessViewController.h"
#import "BDFaceDetectionViewController.h"
#import "BDFaceLivingConfigModel.h"
#import "BDFaceLivingConfigViewController.h"
#import "BDFaceAgreementViewController.h"
#import "BDFaceLogoView.h"
static FlutterResult res;
@implementation FlutterBdfacePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_bdface_plugin"
            binaryMessenger:[registrar messenger]];
    FlutterBdfacePlugin* instance = [[FlutterBdfacePlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
    
  FlutterEventChannel *eventChanel = [FlutterEventChannel eventChannelWithName:@"FlutterBdfacePlugin_stream" binaryMessenger:[registrar messenger]];
    
    [eventChanel setStreamHandler:[[BdTransFlutterStreamManager sharedInstance] streamHandler]];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    res = result;
  if ([@"getPlatformVersion" isEqualToString:call.method]) {

    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }else if([@"detect" isEqualToString:call.method]){
      [self faceDetect: result];
  }
  else if([@"initIosSdk" isEqualToString:call.method]){
      NSLog(@"执行了initSDK方法");
      [self initSDK : call.arguments];
  }else if([@"liveness" isEqualToString:call.method]){
      [self initLivenesswithList];
      [self faceLiveness: result];
  }else {
    result(FlutterMethodNotImplemented);
  }
}
- (void)faceDetect:(FlutterResult) result {
    FlutterAppDelegate* delegate = (FlutterAppDelegate*)[[UIApplication sharedApplication]delegate];
     FlutterViewController* rootVC = (FlutterViewController*)delegate.window.rootViewController;
    NSLog(@"执行了void detect方法");
    [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:@"LiveMode"];
    BDFaceDetectionViewController* dvc = [[BDFaceDetectionViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:dvc];
    navi.navigationBarHidden = true;
    navi.modalPresentationStyle = UIModalPresentationFullScreen;
    [rootVC presentViewController:navi animated:YES completion:nil];
}
- (void)faceLiveness:(FlutterResult) result {
    NSLog(@"执行了void faceLiveness方法");
    [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"LiveMode"];
    FlutterAppDelegate* delegate = (FlutterAppDelegate*)[[UIApplication sharedApplication]delegate];
     FlutterViewController* rootVC = (FlutterViewController*)delegate.window.rootViewController;
    BDFaceLivenessViewController* lvc = [[BDFaceLivenessViewController alloc] init];
    BDFaceLivingConfigModel* model = [BDFaceLivingConfigModel sharedInstance];
    [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:lvc];
    navi.navigationBarHidden = true;
    navi.modalPresentationStyle = UIModalPresentationFullScreen;
    [rootVC presentViewController:navi animated:YES completion:nil];
}

#pragma mark - UI Action
- (void) initSDK:(NSDictionary*) args {
    NSString* licensePath = [NSString stringWithFormat:@"%@.%@", @"idl-license", @"face-ios" ];
    [[FaceSDKManager sharedInstance] setLicenseID:args[@"iosLicId"] andLocalLicenceFile:licensePath andRemoteAuthorize:false];
    
    // 初始化SDK配置参数，可使用默认配置
    // 设置最小检测人脸阈值
    [[FaceSDKManager sharedInstance] setMinFaceSize:200];
    // 设置截取人脸图片高
    [[FaceSDKManager sharedInstance] setCropFaceSizeWidth:400];
    // 设置截取人脸图片宽
    [[FaceSDKManager sharedInstance] setCropFaceSizeHeight:640];
    // 设置人脸遮挡阀值
    [[FaceSDKManager sharedInstance] setOccluThreshold:0.5];
    // 设置亮度阀值
    //[[FaceSDKManager sharedInstance] setIllumThreshold:40];
    // 设置图像模糊阀值
    [[FaceSDKManager sharedInstance] setBlurThreshold:0.3];
    // 设置头部姿态角度
    [[FaceSDKManager sharedInstance] setEulurAngleThrPitch:10 yaw:10 roll:10];
    // 设置人脸检测精度阀值
    [[FaceSDKManager sharedInstance] setNotFaceThreshold:0.6];
    // 设置抠图的缩放倍数
    [[FaceSDKManager sharedInstance] setCropEnlargeRatio:3.0];
    // 设置照片采集张数
    [[FaceSDKManager sharedInstance] setMaxCropImageNum:6];
    // 设置超时时间
    [[FaceSDKManager sharedInstance] setConditionTimeout:15];
    // 设置开启口罩检测，非动作活体检测可以采集戴口罩图片
    [[FaceSDKManager sharedInstance] setIsCheckMouthMask:true];
    // 设置开启口罩检测情况下，非动作活体检测口罩过滤阈值，默认0.8 不需要修改
    [[FaceSDKManager sharedInstance] setMouthMaskThreshold:0.8f];
    // 设置原始图缩放比例
    [[FaceSDKManager sharedInstance] setImageWithScale:0.8f];
    // 设置图片加密类型，type=0 基于base64 加密；type=1 基于百度安全算法加密
    [[FaceSDKManager sharedInstance] setImageEncrypteType:0];
    // 初始化SDK功能函数
    [[FaceSDKManager sharedInstance] initCollect];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(handleNotiChange:) name:@"yiJiBackTo" object:nil];
}

-(void)handleNotiChange:(NSNotification *)sender{
    NSString *val = [[sender userInfo] valueForKey:@"yiJiBackToVal"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:@"true" forKey:@"success"];
    [dic setValue:val forKey:@"srcimage"];
    res(dic);
}

- (void)initLivenesswithList {
    // 默认活体检测打开，顺序执行
    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveEye)];
//    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveMouth)];
//    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveYawRight)];
//    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveYawLeft)];
//    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLivePitchUp)];
//    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLivePitchDown)];
//    [BDFaceLivingConfigModel.sharedInstance.liveActionArray addObject:@(FaceLivenessActionTypeLiveYaw)];
    BDFaceLivingConfigModel.sharedInstance.isByOrder = YES;
    BDFaceLivingConfigModel.sharedInstance.numOfLiveness = 1;
}

- (void) destorySDK{
    // 销毁SDK功能函数
    [[FaceSDKManager sharedInstance] uninitCollect];
}

@end
